#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
#define x first
#define y second

typedef pair<int, int> PII;
const int N = 110;

int n, t;
char g[N][N];
int dist[N][N];

int bfs(PII start, PII end)
{
    queue<PII> q;
    memset(dist, -1, sizeof dist);

    dist[start.x][start.y] = 0;
    q.push(start);

    int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };

    while (q.size())
    {
        auto t = q.front();
        q.pop();

        for (int i = 0; i < 4; i++)
        {
            int x = t.x + dx[i], y = t.y + dy[i];
            if (x < 0 || x >= n || y < 0 || y >= n) continue; // 出界
            if (g[x][y] == '#') continue;  // 障碍物
            if (dist[x][y] != -1) continue;  // 之前已经遍历过

            dist[x][y] = dist[t.x][t.y] + 1;

            if (end == make_pair(x, y)) return dist[x][y];

            q.push({ x, y });
        }
    }

    return -1;
}

int main()
{
    cin >> n >> t;
    for (int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            g[i][j] = '.';
        }
    }

    PII start, end;
    int x, y;

    cin >> x >> y;
    g[x - 1][y - 1] = 'S'; //起点
    start = {x - 1, y - 1};

    cin >> x >> y;
    g[x - 1][y - 1] = 'E'; //终点
    end = {x - 1, y - 1};

    while(t--)
    {
        cin >> x >> y;
        g[x - 1][y - 1] = '#';
    }

    int distance = bfs(start, end);
    if (distance == -1) puts("-1");
    else cout << distance+1;

    return 0;
}
